package net.sureon.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

public class CompanyDAOImpl extends SqlMapClientDaoSupport implements
		CompanyDAO {

	public Company getCompany(long id) {
		return (Company) getSqlMapClientTemplate().queryForObject(
				"getCompanyByID", id);
	}

	public long insertCompany(Company company) {
		Object o = getSqlMapClientTemplate().insert("insertCompany", company);
		if (o instanceof Long) {
			return ((Long) o);
		}
		return 0;
	}

	public int updateCompany(Company company) {
		return getSqlMapClientTemplate().update("updateCompany", company);
	}

	public Company getCompanyBySiteURL(String siteURL) {
		return (Company) getSqlMapClientTemplate().queryForObject(
				"getCompanyBySiteURL", siteURL);
	}

	public List getCompanyConfig(long id) {
		return getSqlMapClientTemplate().queryForList("getCompanyConfig", id);
	}

	public int updateCompanyConfig(Map oneConifg) {
		return getSqlMapClientTemplate().update("updateCompanyConfig",
				oneConifg);
	}

	public void insertCompanyConfig(Map oneConifg) {
		getSqlMapClientTemplate().update("insertCompanyConfig", oneConifg);
	}

	public List<DailyTimingStatistic> getDailyTimingStatisticByCompany(
			long companyID) {
		Company company = new Company();
		company.setCompanyID(companyID);
		return getSqlMapClientTemplate().queryForList(
				"getDailyTimingStatisticByCompany", company);
	}

	public List<DailyTimingStatistic> getDailyTimingStatisticByCompany(
			long companyID, Date startDate, Date endDate) {
		Company company = new Company();
		company.setCompanyID(companyID);
		company.setStartDate(startDate);
		company.setEndDate(endDate);
		return getSqlMapClientTemplate().queryForList(
				"getDailyTimingStatisticByCompany", company);
	}

	public List<Company> getAllCompanies() {
		return getSqlMapClientTemplate().queryForList("getAllCompanies");
	}

	public float getCompanyBalance(long companyID) {
		List l = getSqlMapClientTemplate().queryForList("getCompanyBalance",
				companyID);
		if (l.size() > 0) {
			Object obj = l.get(0);
			if (obj instanceof Float) {
				return ((Float) obj).floatValue();
			}
		}
		return 0f;
	}

	public List<BalanceOperation> getRecentTenBalanceOperation(long companyID) {
		return getSqlMapClientTemplate().queryForList(
				"getRecentTenBalanceOperation", companyID, 0, 10);
	}

	public List<ConsumeRecord> getRecentTenConsumeRecord(long companyID) {
		return getSqlMapClientTemplate().queryForList(
				"getRecentTenConsumeRecord", companyID, 0, 10);
	}

	public void insertBalanceOperation(BalanceOperation operation) {
		getSqlMapClientTemplate().insert("insertBalanceOperation", operation);

	}

	public void insertConsumeRecord(ConsumeRecord record) {
		getSqlMapClientTemplate().insert("insertConsumeRecord", record);
	}

	public Date getLastestConsume(long companyID) {
		return (Date) getSqlMapClientTemplate().queryForObject(
				"getLatestConsumeDate", companyID);
	}

	public RealTimeConsume getCompanyConsumeStatus(long companyID, Date theMonth) {
		Map map = new HashMap();
		map.put("companyID", companyID);
		map.put("theMonth", theMonth);
		return (RealTimeConsume) getSqlMapClientTemplate().queryForObject(
				"getCompanyConsumeStatus", map);
	}

	public List<Company> getCustomerByStatus(String companyStatus) {
		Company company = new Company();
		company.setCompanyStatus(companyStatus);
		return getSqlMapClientTemplate().queryForList("getCustomerByStatus", company);
	}

	public List<ConsumeRecord> getAllCompanyRecentTenConsumeRecord() {
		return getSqlMapClientTemplate().queryForList("getAllCompanyRecentTenConsumeRecord");
	}

	public List<ConsumeRecord> getRecentTenConsumeRecordInRangeMonth(
			Date startMonth, Date endMonth) {
		Map<String,Date> parameter = new HashMap<String,Date>();
		parameter.put("startMonth", startMonth);
		parameter.put("endMonth", endMonth);
		return getSqlMapClientTemplate().queryForList("getRecentTenConsumeRecordInRangeMonth", parameter);
	}

	public List<ConsumeRecord> getConsumeRecordBySiteURLConsumeMonth(
			Date startMonth, Date endMonth, long companyID) {
		Map parameter = new HashMap();
		parameter.put("startMonth", startMonth);
		parameter.put("endMonth", endMonth);
		parameter.put("companyID", companyID);
		return getSqlMapClientTemplate().queryForList("getConsumeRecordBySiteURLConsumeMonth", parameter);
	}

	public List<BalanceOperation> getAllCompanyBalanceOperation() {
		return getSqlMapClientTemplate().queryForList("getAllCompanyBalanceOperation");
	}
	
	public List<BalanceOperation> getBalanceOperationByOperationDate(Date startDate, Date endDate) {
		Map<String, Date> parameter = new HashMap<String, Date>();
		parameter.put("startDate", startDate);
		parameter.put("endDate", endDate);
		return getSqlMapClientTemplate().queryForList("getBalanceOperationByOperationDate", parameter);
	}
	
	public List<BalanceOperation> getBalanceOperationByCompanyIDAndOperationDate(long companyID, Date startDate, Date endDate) {
		Map parameter = new HashMap();
		parameter.put("companyID", companyID);
		parameter.put("startDate", startDate);
		parameter.put("endDate", endDate);
		return getSqlMapClientTemplate().queryForList("getBalanceOperationByCompanyIDAndOperationDate", parameter);
	}
}
